package com.webflux.sample.webfluxapp.controller;

import com.webflux.sample.webfluxapp.entity.MessageEntity;
import com.webflux.sample.webfluxapp.service.MessageService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/info")
@RequiredArgsConstructor
class MessageController {

  private final MessageService messageService;

  /**
   * http://localhost:8080/info/test?name=1
   */
  @GetMapping("/test")
  public Mono<String> testInfo(@RequestParam("name") String name){
    return Mono.just("Hello, " + name + "!");
  }

  /**
   * http://localhost:8080/info
   */
  @GetMapping("/{id}")
  public Mono<MessageEntity> getById(@PathVariable Long id){
    return Mono.just(messageService.findMessageById(id));
  }

  /**
   * http://localhost:8080/info/list-all
   */
  @GetMapping("/list-all")
  public Mono<List<MessageEntity>> listAll(){
    return Mono.just(messageService.findMessages());
  }

  /**
   * http://localhost:8080/info/save
   */
  @PostMapping("/save")
  public Mono<MessageEntity> save(@RequestBody MessageEntity message) {
    return Mono.just(messageService.save(message));
  }

}